package com.kuaimacode.kframework.agent.controller.order;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.services.order.AgentOrderRewardService;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.sys.AgentOrderReward;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * @author 中元
 * @date 2019/04/02
 */
/**
 * Controller
 */
@Controller
@RequestMapping({"/agentOrderReward"})
public class AgentOrderRewardController {
	private static String PAGE_FILE_NAME = "agentOrderReward/";

	@Autowired
	private AgentOrderRewardService agentOrderRewardServices;


	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						ModelMap model) {
		PageInfo<AgentOrderReward> pageInfo = agentOrderRewardServices.findAll(pageNum, pageSize, SessionUtil.getAgentId());
		// pageInfo.list agentOrderReward
		model.put("pageInfo", pageInfo);
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		AgentOrderReward agentOrderReward = new AgentOrderReward();
		map.put("agentOrderReward", agentOrderReward);
		map.put("isAdd", true);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		AgentOrderReward agentOrderReward = agentOrderRewardServices.getById(id);
		map.put("agentOrderReward", agentOrderReward);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(AgentOrderReward agentOrderReward, Boolean isAdd,ModelMap map) {
		try {
			agentOrderReward.setAgentId(SessionUtil.getAgentId());
			if(isAdd){
				// 新建保存
				agentOrderRewardServices.save(agentOrderReward);
			}else{

				// 编辑保存
				agentOrderRewardServices.updateById(agentOrderReward);
			}
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable String id, ModelMap map) {
		AgentOrderReward agentOrderReward = agentOrderRewardServices.getById(id);
		map.put("agentOrderReward", agentOrderReward);
		return PAGE_FILE_NAME+"detail";
	}


	@ResponseBody
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			agentOrderRewardServices.removeById(id);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


}
